<template>
  <div class="container">
    <div class="com-chart" ref="seller_ref"></div>
  </div>
</template>

<script>
export default {
  name: "Seller",
  data(){
    return{
      echartsInstance: null,
      // 服务器返回的数据
      allData: {},
      xSeller: [],  // x轴数据
      ySeller: [],    // y轴数据
      currentPage: 1, // 当前显示页数
      totalPage: 0, // 总页数
      showData: '', // 每页的数据
      timerId: null // 定时器标识
    }
  },
  created() {
    this.$socket.registerCallBack('sellerData',this.getData)
  },
  // 组件创建时
  mounted() {
    this.initChart();
    this.$socket.send({
      action: 'getData',
      socketType: 'sellerData',
      chartName: 'seller',
      value: ''
    })
    window.addEventListener('resize',this.screenAdapter)
    this.screenAdapter()
  },
  // 组件被销毁
  destroyed() {
    clearInterval(this.timerId)
    // 取消屏幕监听
    window.removeEventListener('resize',this.screenAdapter)
    this.$socket.unRegisterCallBack('sellerData',this.getData)
  },
  methods: {
    // 初始化echarts对象
    initChart(){
      // echarts的初始化
      this.echartsInstance = this.$echarts.init(this.$refs.seller_ref, 'chalk')
      // echarts初始设置
      const initEchartsOptions = {
        title: {
          text: '▌ 商家销量统计',
          textStyle: {
            fontSize: 20
          },
          left: 20,
          top: 20
        },
        xAxis: {
          type: 'category',
        },
        yAxis: {
          type: 'value'
        },
        // 鼠标划入文字显示
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line',
            z: 0,
            lineStyle: {
              width: 66,
              color: '#3c3f41'
            }
          }
        },
        series: [
          {
            type: 'bar',
            barWidth: 66,
            label: {
              show: true,
              position: 'top',
              textStyle: {
                color: 'white',
              }
            },
            itemStyle: {
              barBorderRadius: [8, 8, 0, 0],
              // 颜色渐变
              color: new this.$echarts.graphic.LinearGradient(0,1,1,0,[
                {
                  offset: 0,
                  color: '#72afd3'
                },
                {
                  offset: 1,
                  color: '#37ecba'
                }
              ])
            }
          }
        ]
      }
      this.echartsInstance.setOption(initEchartsOptions)
      // echarts事件监听
      this.echartsInstance.on('mousemove', () => {
        clearInterval(this.timerId)
      })
      this.echartsInstance.on('mousemove', () => {
        this.startInterval()
      })
    },
    // 获取数据
    getData(resData){
      this.allData = resData.sort((a, b) => {
        return a.value - b.value
      })
      this.totalPage = this.allData.length % 5 === 0 ? this.allData.length / 5 : this.allData.length / 5 + 1
      this.updateChart()
      this.startInterval()
    },
    // 更新数据
    updateChart(){
      this.showData = this.allData.slice((this.currentPage - 1) * 5, this.currentPage * 5)
      this.xSeller = this.showData.map((item) => {
        return item.name
      })
      this.ySeller = this.showData.map((item) => {
        return item.value
      })
      const dataOption = {
        xAxis: {
          data: this.xSeller
        },
        series: [
          {
            data: this.ySeller,
          }
        ]
      };
      this.echartsInstance.setOption(dataOption)
    },
    // 定时改变数据
    startInterval(){
      this.timerId = setInterval(() => {
        this.currentPage++
        if (this.currentPage > this.totalPage){
          this.currentPage = 1
        }
        this.updateChart()
      },3000)
    },
    // 自适应
    screenAdapter(){
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: 20
          }
        },
        // 鼠标划入文字显示
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line',
            z: 0,
            lineStyle: {
              width: this.$refs.seller_ref.offsetWidth / 100 * 5  ,
              color: '#3c3f41'
            }
          }
        },
        series: [
          {
            type: 'bar',
            barWidth:  this.$refs.seller_ref.offsetWidth / 100 * 5
          }
        ]
      }
      this.echartsInstance.setOption(adapterOption)
      this.echartsInstance.resize()
    }
  }
}
</script>

<style scoped>

</style>